package com.ncloud.system.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.system.business.RoleBusi;
import com.ncloud.system.entity.Role;
import com.ncloud.system.entity.RoleDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by shiqiang on 2017/7/31.
 */
@Api(value = "自动化运维角色管理", description = "角色管理", tags = "角色管理",
        basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/sys")
public class RoleController {
    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private RoleBusi roleBusi;
    /*生成菜单*/
    @ApiOperation(value="create role", notes="角色创建")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "部门", value = "角色信息", required = true, dataType = "Role" )
    })
    @PostMapping(value = "/role")
    public Result<Object> saveRole(HttpServletRequest request, @RequestBody Role json) throws Exception {
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.setCreateUser(Long.parseLong(userID.toString()));
        }
        if(logger.isDebugEnabled()){
            logger.debug("role info={%s}"+ JSON.toJSONString(json));
        }
        roleBusi.addRole(json);
        return ResultUtil.success();
    }


    @ApiOperation(value="update role", notes="角色修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String"),
            @ApiImplicitParam(name = "角色", value = "角色信息", required = true, dataType = "Role" )
    })
    @PutMapping(value = "/role")
    public Result<Object> updateRole(HttpServletRequest request, @RequestBody Role json) throws Exception {
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.setUpdateUser(Long.parseLong(userID.toString()));
        }
        logger.info("depart info={%s}"+ JSON.toJSONString(json));
        roleBusi.modifyRole(json);
        return ResultUtil.success();
    }
    @ApiOperation(value="deleteRole", notes="根据roleId，删除其对应角色")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String"),
    })
    @DeleteMapping(value = "/role/{companyId}/{roleId}")
    public Result<Object> deleteRole(@PathVariable("companyId") Long companyId,@PathVariable("roleId") Long roleId) throws Exception {
        roleBusi.deleteRole(companyId,roleId);
        return ResultUtil.success();
    }
    /*根据工号对应的权限和公司查询对应的分组信息*/
    @ApiOperation(value="getAllRoles", notes="获取其所有分组")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String")
    })
    @GetMapping(value = "/role/allRoles")
    public Result<List<RoleDetail>> getAllRoles(HttpServletRequest request) throws Exception {
        Object userID = request.getAttribute("userID");

        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            List<RoleDetail> roles = roleBusi.getRoleInfo(Long.parseLong(userID.toString()));
            return ResultUtil.success(roles);
        }
    }

    /*根据角色id获取详细信息*/
    @ApiOperation(value="getRolesById", notes="根据roleId获取其对应的详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header",name = "X-CSRF-TOKEN" ,required = true,dataType = "String"),
    })
    @GetMapping(value = "/role/detail/{roleId}")
    public Result<JSONObject> getRolesById(HttpServletRequest request, @PathVariable("roleId") Long roleId) throws Exception {
        JSONObject role = roleBusi.getRoleDetailJson(roleId);
        return ResultUtil.success(role);
    }

}
